package his.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import his.entity.ExpenseClass;
import his.service.ExpenseClassService;
import org.apache.ibatis.annotations.Delete;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/expense/expense_class/")
public class ExpenseClassController {
    @Autowired
    ExpenseClassService expenseClassService;

    @GetMapping("get_search/{KeyWord}")
    @ResponseBody
    public CommonResponse<List<ExpenseClass>> get_search_expenseclass(@PathVariable String KeyWord){
        return expenseClassService.get_select_expenseclass(KeyWord);
    }

    @PostMapping("insert_new")
    @ResponseBody
    public  CommonResponse<ExpenseClass> insert_new_expenseclass(HttpServletRequest httpServletRequest){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String ExpCode = (String) jsonObject.get("ExpCode");
        String ExpName = (String) jsonObject.get("ExpName");
        return expenseClassService.insert_new_expenseclass(ExpCode , ExpName);
    }


    @PostMapping("update")
    @ResponseBody
    public  CommonResponse<ExpenseClass>  update_expenseclass(HttpServletRequest httpServletRequest){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        int id = (int)jsonObject.get("id");
        String ExpCode = (String) jsonObject.get("ExpCode");
        String ExpName = (String) jsonObject.get("ExpName");
        return expenseClassService.update_expenseclass( id ,  ExpCode , ExpName);
    }

    @DeleteMapping("delete/{id}")
    @ResponseBody
    public CommonResponse<String> delete_expenseclass(@PathVariable Integer id){
        return expenseClassService.delete_expenseclass(id);
    }
}
